package dk.eclipse.android.badassapp.distance;

import java.util.Collection;

import com.google.android.maps.GeoPoint;

/**
 * 
 * 
 *         Calculate the current rectangle covering you and the target(s).
 */
public class MapRectangle {
	int leftmostE6;
	int rightmostE6;
	int lowestE6;
	int highestE6;

	public MapRectangle(Collection<ObjectOnMap> values) {

		int count = 0;
		for (ObjectOnMap objectOnMap : values) {
			if (count == 0) {
				leftmostE6 = (int) (objectOnMap.mLongitude * 1e6);
				rightmostE6 = (int) (objectOnMap.mLongitude * 1e6);
				lowestE6 = (int) (objectOnMap.mLatitude * 1e6);
				highestE6 = (int) (objectOnMap.mLatitude * 1e6);
			}
			leftmostE6 = Math.min((int) (objectOnMap.mLongitude * 1e6),
					leftmostE6);
			rightmostE6 = Math.max((int) (objectOnMap.mLongitude * 1e6),
					rightmostE6);
			lowestE6 = Math.min((int) (objectOnMap.mLatitude * 1e6),
					lowestE6);
			highestE6 = Math.max((int) (objectOnMap.mLatitude * 1e6),
					highestE6);

			count++;
		}
	}

	public GeoPoint getCenter() {
		int longNormalize = 0;
		int latNormalize = 0;

		if (leftmostE6 < 0) {
			longNormalize = -1 * leftmostE6;
		}
		if (lowestE6 < 0) {
			latNormalize = -1 * lowestE6;
		}

		int longitude = leftmostE6
				+ (((rightmostE6 + longNormalize) - (leftmostE6 + longNormalize)) / 2);
		int latitude = lowestE6
				+ (((highestE6 + latNormalize) - (lowestE6 + latNormalize)) / 2);
		return new GeoPoint(latitude, longitude);
	}

	public int getLatitudeSpan() {
		return highestE6 - lowestE6;
	}

	public int getLongitudeSpan() {
		return rightmostE6 - leftmostE6;
	}

}
